home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr48 / pasclern.zip / CHAP10.TXT < prev    next >
Text File  |  1993-04-01  |  13KB  |  323 lines

  1.                     CHAPTER 10 - Standard Input/Output
  2.  
  3.  
  4.  
  5.             During  the  course of this tutorial we have been  using 
  6.         the WRITE and WRITELN procedures to display data,  and it is 
  7.         now time to discuss them fully.  Actually there is little to 
  8.         be said that has not already been said,  but in order to get 
  9.         all  of the data in one place they will be  redefined  here.  
  10.         As  mentioned  earlier,  WRITE and WRITELN are not  actually 
  11.         reserved words but are procedure calls.   They are therefore 
  12.         merely  identifiers that could be changed but  there  should 
  13.         never  be  a  reason to do so.   Lets get on  to  our  first 
  14.         example program WRITELNX which has lots of output.
  15.  
  16.                           MANY OUTPUT STATEMENTS
  17.  
  18.             Pascal  has two output statements that are only slightly 
  19.         different  in  the way they  work.   The  WRITELN  statement 
  20.         outputs  all of the data specified within it,  then  returns 
  21.         the  cursor  to the beginning of the next line.   The  WRITE 
  22.         statement outputs all of the data specified within it,  then 
  23.         leaves  the  cursor at the next character  where  additional 
  24.         data  can be output.   The WRITE statement can therefore  be 
  25.         used  to  output a line in bits and pieces  if  desired  for 
  26.         programming convenience.  The first example program for this 
  27.         chapter,  WRITELNX,  has  many  output statements  for  your 
  28.         observation.   All  outputs are repeated so you can  observe 
  29.         where the present field ends and the next starts.
  30.  
  31.             Observe the INTEGER output statements.  The first simply 
  32.         directs the system to output "index" twice, and it does with 
  33.         no  separating blanks.   The second statement says to output 
  34.         "index" twice also,  but it instructs the system to put each 
  35.         output  in  a field 15 characters wide with the  data  right 
  36.         justified  in the field.   This makes the output  look  much 
  37.         better.   This  illustrates  that you have complete  control 
  38.         over the appearance of your output data.
  39.  
  40.             The  REAL output statements are similar to  the  integer 
  41.         except  that when the data is put into a field 15 characters 
  42.         wide,  it is still displayed in scientific format.  Adding a 
  43.         second field descriptor tells the system how many digits you 
  44.         want displayed after the decimal point.   The next few lines 
  45.         illustrate the second field and its use.
  46.  
  47.             The BOOLEAN,  CHAR,  and STRING examples should be  self 
  48.         explanatory.   Notice  that when the string is output,  even 
  49.         though  the  string  has been defined as  a  maximum  of  10 
  50.         characters,  it  has  been  assigned  a  string  of  only  8 
  51.         characters, so only 8 characters are output.
  52.  
  53.  
  54.  
  55.  
  56.  
  57.                                   Page 46
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                     CHAPTER 10 - Standard Input/Output
  68.  
  69.  
  70.                    NOW FOR SOME INPUT FROM THE KEYBOARD
  71.  
  72.             The  example  file READINT will illustrate reading  some 
  73.         integer data from the keyboard.  A message is output with an 
  74.         interesting fact that should be pointed out.  Anyplace where 
  75.         Pascal  uses  a string variable or  constant,  it  uses  the 
  76.         apostrophe  for  a  delimiter.    Thus,  anyplace  where  an 
  77.         apostrophe is used in a string, it will end the string.  Two 
  78.         apostrophes   in  a  row  will  be  construed  as  a  single 
  79.         apostrophe  within  the string and will  not  terminate  the 
  80.         string.  The term 'READ' within the string will therefore be 
  81.         displayed as shown earlier in this sentence.
  82.  
  83.             The  variable "index" is used to loop five times through 
  84.         a sequence of statements with one READ statement in it.  The 
  85.         three integer variables are read in and stored with the  one 
  86.         statement.   If  less than three are input in the statement, 
  87.         only as many as are read in will be defined,  the rest  will 
  88.         be unchanged.  Following completion of the first loop, there 
  89.         is a second loop that will be executed 5 times with only one 
  90.         minor  change,  the READ statement is replaced by the READLN 
  91.         statement.   At this point it would be best run this program 
  92.         trying several variations with input data.
  93.  
  94.             When  you run READINT,  it will request three  integers.  
  95.         Reply with three small integers of your choice with as  many 
  96.         blank  spaces  between  each as you desire,  followed  by  a 
  97.         carriage  return.   The system will echo your three  numbers 
  98.         back  out,  and request three more.   Respond with only  one 
  99.         number  this time,  different from each of the first  three.  
  100.         You  will  get  your new number followed  by  your  previous 
  101.         second and third number indicating that you did not re-enter 
  102.         the last two integer variables.  Enter all three again, this 
  103.         time  including a negative number and observe the echo  once 
  104.         again.
  105.  
  106.             Continue  entering numbers until the system outputs  the 
  107.         message  indicating that it will now be using  the  'READLN' 
  108.         for reading data.  At this point enter the same numbers that 
  109.         you  did in the previous section and notice the  difference, 
  110.         which is only very slight.   Each time you hit the enter key 
  111.         to  cause  the  computer to process the data you  have  just 
  112.         given it,  it will echo the carriage return to the  display, 
  113.         and  the  "Thank you" message will be on a new  line.   When 
  114.         entering data from the keyboard, the only difference in READ 
  115.         and  READLN is whether or not the carriage return is  echoed 
  116.         to the display following the data read operation.
  117.  
  118.             It should not be a surprise to you that after you  enter 
  119.         the  data,  the data is stored within the program and can be 
  120.  
  121.  
  122.  
  123.                                   Page 47
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                     CHAPTER 10 - Standard Input/Output
  134.  
  135.  
  136.         used anywhere that integer data is legal for use.  Thus, you 
  137.         could read in an integer, and use the integer to control the 
  138.         number of times through a loop, as a CASE selector, etc.
  139.  
  140.                         TIME TO CRASH THE COMPUTER
  141.  
  142.             Crashing the computer will not hurt a thing.   Rerun the 
  143.         above  program and instead of entering integer  data,  enter 
  144.         some real data with decimal points,  or even some  character 
  145.         data.   The  computer  should display some kind  of  message 
  146.         indicating that you have caused an I/O error (Input/Output), 
  147.         and  most  implementations  of Pascal  will  probably  abort 
  148.         operation  (that simply means to stop the program and return 
  149.         control  to the operating system).   No harm has been  done, 
  150.         simply start it again to enter more numbers or errors.
  151.  
  152.                            READING REAL NUMBERS
  153.  
  154.             The example program READREAL will illustrate how to read 
  155.         real numbers into the computer.  It will read an integer and 
  156.         three  real  numbers  each time through  the  loop.   It  is 
  157.         perfectly fine to give the system a number without a decimal 
  158.         point for a real number.   The computer will simply read  it 
  159.         as  a decimal number with zeros after the decimal point  and 
  160.         consider it as a real number internally. As you found out in 
  161.         the last example program,  however, it is not permissible to 
  162.         include  a  decimal  point in the data if  the  computer  is 
  163.         looking  for  an integer variable.   Include some  character 
  164.         data for a real number and crash the system in this  program 
  165.         too.
  166.  
  167.                           READING CHARACTER DATA
  168.  
  169.             The  next example program,  READCHAR,  will read in  one 
  170.         character each time through the loop and display it for you.  
  171.         Try  entering more than one character and you will see  that 
  172.         the  extra  characters will simply be ignored.   It  is  not 
  173.         possible  to  crash this program because any  character  you 
  174.         enter will be valid.
  175.  
  176.             The  next example,  READARRY,  will read in a string  of 
  177.         characters  and display them for you.   Up to 10  characters 
  178.         will be read, and if less than 10 are read, the rest will be 
  179.         blank filled.   Try entering 10 characters,  then 4,  to see 
  180.         that  the  residual  6  characters are  blanked  out  before 
  181.         storing  and  printing.   Since the array is  fixed  at  ten 
  182.         characters, ten characters are always printed out, including 
  183.         trailing blanks.
  184.  
  185.  
  186.  
  187.  
  188.  
  189.                                   Page 48
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                     CHAPTER 10 - Standard Input/Output
  200.  
  201.  
  202.             Finally READSTRG will also read up to 10 characters, but 
  203.         since  a string is a dynamic length variable,  it will  only 
  204.         print  out  the characters you input each time,  up  to  the 
  205.         maximum  of 10 as defined in the VAR declaration.   It  will 
  206.         display  trailing blanks if you type them in because  blanks 
  207.         are valid characters.
  208.  
  209.                          BULLET PROOF PROGRAMMING
  210.  
  211.             It  can be frustrating to be running a program and  have 
  212.         it  declare  an  I/O error and  terminate  operation  simply 
  213.         because  you  have  entered  an  incorrect  character.   The 
  214.         integer and real data inputs defined earlier in this chapter 
  215.         are   fine   for  quick  little  programs  to  do   specific 
  216.         calculations,  but  if you are writing a large  applications 
  217.         program  it is better to use another technique.   Since  the 
  218.         character  and string inputs cannot abort operation  of  the 
  219.         program,  it  is best to use them to input the variable data 
  220.         and  check  the  data  internally  under  your  own  program 
  221.         control.  An error message can then be given to the operator 
  222.         and  another opportunity granted to input the correct  data.  
  223.         All  well  written  large  application  programs  use   this 
  224.         technique.
  225.  
  226.                  HOW DO I PRINT SOMETHING ON THE PRINTER
  227.  
  228.             With all of the Pascal knowledge you now have, it is the 
  229.         simplest thing in the world to get data to the printer.  The 
  230.         example  file  PRINTOUT will show you graphically how to  do 
  231.         it.   Every WRITE or WRITELN statement is required to have a 
  232.         device identifier prior to the first output field.  If there 
  233.         is  none,  it  is automatically defaulted  to  the  standard 
  234.         output device, the display monitor.  The example program has 
  235.         a  few  outputs  to the monitor with the  device  identifier 
  236.         included,  namely "output".   This is only done to show  you 
  237.         the  general form of the WRITE statements.   There are  also 
  238.         many  statements in this program with the display identifier 
  239.         "lst",  which is the standard name for the "list" device  or 
  240.         the printer.  Compile and run this program with your printer 
  241.         turned on for some printer output.
  242.  
  243.             Just  to supply you with a bit more  information,  every 
  244.         READ  and READLN statement is also required to have a device 
  245.         identifier  prior  to the first input  field.   As  you  may 
  246.         suspect,  it  is  also  defaulted  to  "input"  if  none  is 
  247.         specified, and the standard input device is the keyboard.
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.                                   Page 49
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                     CHAPTER 10 - Standard Input/Output
  266.  
  267.  
  268.                            PROGRAMMING EXERCISE
  269.  
  270.         1.  Write a program containing a loop to read in a character 
  271.             string  up to 60 characters long,  then print the string 
  272.             on your printer. When you run the program, you will have 
  273.             the  simplest word processing program in the  world.  Be 
  274.             sure  to  include a test to end the loop,  such as  when 
  275.             "END" is typed in.
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.                                   Page 50
  322.  
  323.